package com.example.shop_2302.catexq

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2302.R
import com.example.shop_2302.adapter.GoodsAdapter
import com.example.shop_2302.databinding.ActivityCateXqBinding

@Route(path = "/app/CateXqActivity")
class CateXqActivity : BaseActivity<ActivityCateXqBinding,CateXqViewModel>() {
    @Autowired
    @JvmField
    var cname = ""
    @Autowired
    @JvmField
    var cid = 0

    var keyword = ""//搜索关键词
    var limit = 10
    var news = false//是否是新品
    var page = 1
    var priceOrder = ""//价格排序 //升序 asc  降序 desc  无序 “”
    var salesOrder = ""//销量排序

    lateinit var goodsAdapter: GoodsAdapter

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.tvType.text = cname

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        //瀑布流
        //binding.rvGoods.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        binding.rvGoods.layoutManager = GridLayoutManager(this,2)
       viewModel.cateXq(cid, keyword, limit, news, page, priceOrder, salesOrder)

        //点击新品  第一次点击变蓝色  再次点击 变黑色
        binding.tvIsnew.setOnClickListener {
            if(news){//就是新品   文字蓝色    点击变为黑色
                binding.tvIsnew.setTextColor(Color.BLACK)
                news = false
            }else{
                binding.tvIsnew.setTextColor(Color.BLUE)
                news = true
            }
            viewModel.cateXq(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //点击价格排序，第一次点击，图标变成升序  第二次点击 图标变成降序   第三次点击 图标变成无序  //无论点击几次  销量的图标永远是无序
        binding.tvPrice.setOnClickListener {
            if(priceOrder==""){
                priceOrder = "asc"
                //修改tv右侧的图标
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.sheng,0)
            }else if(priceOrder=="asc"){
                priceOrder = "desc"
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.jiang,0)
            }else{
                priceOrder = ""
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.wu,0)
            }
            //销量无序
            binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.wu,0)
            salesOrder = ""

            viewModel.cateXq(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //点击销量排序，第一次点击，图标变成升序  第二次点击 图标变成降序   第三次点击 图标变成无序  //无论点击几次  价格的图标永远是无序
        binding.tvSale.setOnClickListener {
            if(salesOrder ==""){
                salesOrder = "asc"
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.sheng,0)

            }else if(salesOrder =="asc"){
                salesOrder ="desc"
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.jiang,0)
            }else{
                salesOrder = ""
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.wu,0)
            }

            priceOrder = ""
            binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.wu,0)

            viewModel.cateXq(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }

        //et文本监听
        binding.etSearch.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                keyword = s.toString()//目的：但点击其他排序文字时，数据不会发生变化
                viewModel.cateXq(cid,keyword, limit, news, page, priceOrder, salesOrder)
                //查询所有商品//无接口
              /*  val goodsList = goodsAdapter.data.filter {
                    keyword in it.storeName//意思是 商品名称中有 keyword
                }
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(goodsList)
                goodsAdapter.notifyDataSetChanged()*/

            }

            override fun afterTextChanged(s: Editable?) {

            }
        })
        //点击商品列表，跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/XqActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }



    }

    override fun initData() {
        viewModel.cateXqSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun getViewModel(): Class<CateXqViewModel> = CateXqViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_cate_xq

}